package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.shopManager.domain.MetaverseShopManager;
import com.ruoyi.system.domain.SysUserPost;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.group.domain.MetaverseGroup;
import com.ruoyi.group.service.IMetaverseGroupService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 集团（B端）Controller
 * 
 * @author ruoyi
 * @date 2023-11-01
 */
@RestController
@RequestMapping("/metaverse-group/group")
public class MetaverseGroupController extends BaseController
{
    @Autowired
    private IMetaverseGroupService metaverseGroupService;
    @Autowired
    private ISysUserService userService;

    /**
     * 查询集团（B端）列表
     */
    @PreAuthorize("@ss.hasPermi('metaverse-group:group:list')")
    @GetMapping("/list")
    public TableDataInfo list(MetaverseGroup metaverseGroup)
    {
        startPage();
        List<MetaverseGroup> list = metaverseGroupService.selectMetaverseGroupList(metaverseGroup);
        return getDataTable(list);
    }

    /**
     * 导出集团（B端）列表
     */
    @PreAuthorize("@ss.hasPermi('metaverse-group:group:export')")
    @Log(title = "集团（B端）", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MetaverseGroup metaverseGroup)
    {
        List<MetaverseGroup> list = metaverseGroupService.selectMetaverseGroupList(metaverseGroup);
        ExcelUtil<MetaverseGroup> util = new ExcelUtil<MetaverseGroup>(MetaverseGroup.class);
        util.exportExcel(response, list, "集团（B端）数据");
    }

    /**
     * 获取集团（B端）详细信息
     */
    @PreAuthorize("@ss.hasPermi('metaverse-group:group:query')")
    @GetMapping(value = "/{groupId}")
    public AjaxResult getInfo(@PathVariable("groupId") Long groupId)
    {
        return success(metaverseGroupService.selectMetaverseGroupByGroupId(groupId));
    }

    /**
     * 新增集团（B端）
     */
    @PreAuthorize("@ss.hasPermi('metaverse-group:group:add')")
    @Log(title = "集团（B端）", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody MetaverseGroup metaverseGroup)
    {
        List<MetaverseGroup> groupList = metaverseGroupService.selectMetaverseGroupByGroupAccount(metaverseGroup.getGroupAccount());
        if (groupList.size()>0){
            return error("新增集团失败，" + metaverseGroup.getGroupAccount() + "用户已存在！");
        }
        metaverseGroup.setCreateBy(getUsername());
        int num = metaverseGroupService.insertMetaverseGroup(metaverseGroup);
        if (num>0){
            SysUser user = new SysUser();
            long dep = 101;
            user.setUserName(metaverseGroup.getGroupAccount());
            user.setPassword(SecurityUtils.encryptPassword(metaverseGroup.getGroupPassword()));
            user.setNickName(metaverseGroup.getGroupName());
            user.setSex("2");
            user.setPhonenumber(metaverseGroup.getPhonenumber());
            user.setDeptId(dep);
            user.setCreateBy(getUsername());
            user.setHostId(metaverseGroup.getGroupId());
            userService.insertUser(user);

            long roleId = 2;
            SysUserRole userRole = new SysUserRole();
            userRole.setUserId(user.getUserId());
            userRole.setRoleId(roleId);
            userService.insertUserAuthRole(userRole);

            long postId = 1;
            SysUserPost userPost = new SysUserPost();
            userPost.setPostId(postId);
            userPost.setUserId(user.getUserId());
            userService.insertUserAuthPost(userPost);

        }
        return toAjax(num);
    }

    /**
     * 修改集团（B端）
     */
    @PreAuthorize("@ss.hasPermi('metaverse-group:group:edit')")
    @Log(title = "集团（B端）", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MetaverseGroup metaverseGroup)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        metaverseGroup.setUpdateBy(user.getUserName());
        return toAjax(metaverseGroupService.updateMetaverseGroup(metaverseGroup));
    }

    /**
     * 删除集团（B端）
     */
    @PreAuthorize("@ss.hasPermi('metaverse-group:group:remove')")
    @Log(title = "集团（B端）", businessType = BusinessType.DELETE)
	@DeleteMapping("/{groupIds}")
    public AjaxResult remove(@PathVariable Long[] groupIds)
    {
        int num = metaverseGroupService.deleteMetaverseGroupByGroupIds(groupIds);

        if (num>0){

        }

        return toAjax(num);
    }
}
